package com.aidex.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.aidex.biz.domain.*;
import com.aidex.biz.service.*;
import com.aidex.common.annotation.DataScope;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.mapper.BizAssessInfoMapper;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 考核信息Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2024-11-29
 */
@Service
@Transactional(readOnly = true)
public class BizAssessInfoServiceImpl extends BaseServiceImpl<BizAssessInfoMapper, BizAssessInfo> implements BizAssessInfoService
{
    private static final Logger log = LoggerFactory.getLogger(BizAssessInfoServiceImpl.class);

    @Autowired
    private BizAssessToDeptService assessToDeptService;

    @Autowired
    private BizAssessDetailsInfoService assessDetailsInfoService;
    @Autowired
    private BizAssessToDeptDetailsService assessToDeptDetailsService;

    @Autowired
    private DaibanIngService daibanIngService;

    /**
     * 获取单条数据
     * @param bizAssessInfo 考核信息
     * @return 考核信息
     */
    @Override
    public BizAssessInfo get(BizAssessInfo bizAssessInfo)
    {
        BizAssessInfo dto = super.get(bizAssessInfo);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 考核信息id
     * @return 考核信息
     */
    @Override
    public BizAssessInfo get(String id)
    {
        BizAssessInfo dto = super.get(id);
        return dto;
    }


    /**
     * 查询考核信息列表
     * @param bizAssessInfo 考核信息
     * @return 考核信息
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<BizAssessInfo> findList(BizAssessInfo bizAssessInfo)
    {
        return super.findList(bizAssessInfo);
    }

    /**
     * 分页查询考核信息列表
     * @param bizAssessInfo 考核信息
     * @return 考核信息
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<BizAssessInfo> findPage(BizAssessInfo bizAssessInfo)
    {
        return super.findPage(bizAssessInfo);
    }

    /**
     * 保存考核信息
     * @param bizAssessInfo
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public boolean save(BizAssessInfo bizAssessInfo)
    {
        if(super.save(bizAssessInfo)){
            // 删除 部门考核表
            BizAssessToDept bizAssessToDept = new BizAssessToDept();
            bizAssessToDept.setAssessId(bizAssessInfo.getId());
            assessToDeptService.deleteByAssessId(bizAssessToDept);
            // 重新建立 部门考核表信息
            bizAssessToDept.setAssessTitle(bizAssessInfo.getTitle());
            bizAssessToDept.setDeptIds(bizAssessInfo.getDeptIds());
            bizAssessToDept.setEndTime(bizAssessInfo.getEndTime());
            bizAssessToDept.setAssessId(bizAssessInfo.getId());
            assessToDeptService.insertBatch(bizAssessToDept);
            return true;
        }
        return false;
    }


    /**
     * 删除考核信息信息
     * @param bizAssessInfo
     * @return 结果
     */
    @Override
    public boolean remove(BizAssessInfo bizAssessInfo)
    {
        return super.remove(bizAssessInfo);
    }

    /**
     * 批量删除考核信息
     * @param ids 需要删除的考核信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizAssessInfoByIds(String[] ids)
    {
        return mapper.deleteBizAssessInfoByIds(ids);
    }

    /**
     * 获取最大编号
     * @param bizAssessInfo 考核信息
     * @return 结果
     */
    @Override
    public int findMaxSort(BizAssessInfo bizAssessInfo)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(bizAssessInfo));
    }

    @Override
    @Transactional(readOnly = false)
    public Boolean fabu(BizAssessInfo bizAssessInfo) {
        if (true){
            BizAssessToDept bizAssessToDept= new BizAssessToDept();
            bizAssessToDept.setAssessId(bizAssessInfo.getId());
            List<BizAssessToDept> assessToDepts = assessToDeptService.findListByFu(bizAssessToDept);
            BizAssessDetailsInfo bizAssessDetailsInfo=new BizAssessDetailsInfo();
            bizAssessDetailsInfo.setAssessId(bizAssessInfo.getId());
            List<BizAssessDetailsInfo> assessDetailsInfos = assessDetailsInfoService.findList(bizAssessDetailsInfo);
            if (assessDetailsInfos.size()==0){
                return false;
            }
            List<BizAssessToDeptDetails> assessToDeptDetails = new ArrayList<>();
            List<DaibanIng> daibanIngs = new ArrayList<>();
            for (BizAssessToDept assessToDept : assessToDepts) {
                for (BizAssessDetailsInfo assessDetailsInfo : assessDetailsInfos) {
                    BizAssessToDeptDetails bizAssessToDeptDetails=new BizAssessToDeptDetails();
                    bizAssessToDeptDetails.setAssessDetailsId(assessDetailsInfo.getId());
                    bizAssessToDeptDetails.setDeptId(assessToDept.getDeptId());
                    bizAssessToDeptDetails.setAssessId(bizAssessInfo.getId());
                    bizAssessToDeptDetails.setAssessContent(assessDetailsInfo.getAssessContent());
                    bizAssessToDeptDetails.setAssessItem(assessDetailsInfo.getAssessItem());
                    bizAssessToDeptDetails.setAssessMark(assessDetailsInfo.getAssessMark());
                    bizAssessToDeptDetails.setAssessMaxScore(assessDetailsInfo.getAssessScore());
                    bizAssessToDeptDetails.setAssessTitle(assessDetailsInfo.getAssessTitle());
                    bizAssessToDeptDetails.setAssessScore(new BigDecimal(0));
                    bizAssessToDeptDetails.setUserId(assessToDept.getUserId());
                    bizAssessToDeptDetails.setUserName(assessToDept.getUserName());
                    bizAssessToDeptDetails.setAssessDept(assessToDept.getAssessDept());
                    bizAssessToDeptDetails.setAssessDeptId(assessToDept.getId());
                    bizAssessToDeptDetails.setDelFlag("0");
                    assessToDeptDetails.add(bizAssessToDeptDetails);
                    DaibanIng daibanIng = new DaibanIng();
                    daibanIng.setContent("("+assessToDept.getAssessTitle()+")"+"的考核");
                    daibanIng.setType("日常考核");
                    daibanIng.setDeptId(assessToDept.getDeptId());
                    daibanIng.setRouter("day_assess/myAssessInfo");
                    daibanIng.setPath("biz/bizassesstodept/myAssessInfo");
                    daibanIng.setStatus("0");
                    daibanIngs.add(daibanIng);
                }

            }

            assessToDeptDetailsService.insertBatch(assessToDeptDetails);
            // 更新状态
            bizAssessToDept.setStatus("1");
            assessToDeptService.updateStatus(bizAssessToDept);
            // 添加待办
            daibanIngService.insertBatch(daibanIngs);
            // 保存
            super.save(bizAssessInfo);
            return true;
        }
        return false;
    }

    @Override
    @Transactional(readOnly = false)
    public int updateStatus(BizAssessInfo bizAssessInfo) {
        return mapper.updateStatus(bizAssessInfo);
    }
}
